<?php
namespace app\cms\controller;

use app\common\controller\Front;

class Tag extends Front
{
    private $items = [];
    
    public function _initialize()
    {
        //请求验证
        if( !cmsRequestCheck($this->request->ip(),$this->request->header('user-agent')) ){
            $this->error(lang('cmsErrorRest'), 'cms/index/index');
        }
        parent::_initialize();
    }
    
    public function index()
    {
        //查询数据
        $term = $this->detail();
        //变量赋值
        $this->assign($term);
        //数据列表
        $this->assign($this->items);
        //自定义模板
        if($term['term_tpl']){
            return $this->fetch($term['term_tpl']);
        }
        //默认加载
        return $this->fetch();
    }
    
    //空操作
    public function _empty($action='')
    {
        //允许的操作名
        if(!in_array($action,explode(',',config('cms.action_name')))){
            $this->error(lang('cmsErrorParams'));
        }
        //查询数据
        $term = $this->detail();
        //变量赋值
        $this->assign($term);
        //数据列表
        $this->assign($this->items);
        //自定义模板
        if($term['term_tpl']){
            return $this->fetch($term['term_tpl']);
        }
        //默认加载
        return $this->fetch();
    }
    
    //公共
    private function detail()
    {
        //最大偏移量
        if(config('cms.page_max_'.$this->site['action']) && $this->site['page'] > config('cms.page_max_'.$this->site['action'])){
            $this->error(lang('cmsErrorMax'));
        }
        //标签详情
        if( isset($this->query['id']) ){
            $term = cmsTagId($this->query['id']);
        }elseif( isset($this->query['slug']) ){
            $term= cmsTagSlug($this->query['slug'],true,'normal',$this->site['action']);
        }elseif( isset($this->query['name']) ){
            $term = cmsTagName($this->query['name'],true,'normal',$this->site['action']);
        }else{
            $this->error(lang('cmsErrorParams'),'cms/index/index');
        }
        //数据为空
        if(!$term){
            $this->error(lang('cmsErrorEmpty'),'cms/index/index');
        }
        //分页路径
        $term['pagePath']       = cmsUrlTag($term,'[PAGE]');
        //地址栏
        $term['pageSize']       = intval(config('cms.limit_tag_'.$this->site['action']));
        $term['pageNumber']     = $this->site['page'];
        $term['sortName']       = DcEmpty(config('cms.sort_tag_'.$this->site['action']),'detail_id');
        $term['sortOrder']      = 'desc';
        //SEO标签
        $term['seoTitle']       = cmsSeo(DcEmpty($term['term_title'],$term['term_name']),$this->site['page']);
        $term['seoKeywords']    = cmsSeo(DcEmpty($term['term_keywords'],$term['term_name']),$this->site['page']);
        $term['seoDescription'] = cmsSeo(DcEmpty($term['term_description'],$term['term_name']),$this->site['page']);
        //自定义分页
        if(is_numeric($term['term_limit'])){
            $term['pageSize'] = $term['term_limit'];
        }
        //数据列表
        if($term['pageSize']){
            $this->items = cmsSelect([
                'cache'   => true,
                'term_id' => $term['term_id'],
                'status'  => 'normal',
                'action'  => $this->site['action'],
                'controll'=> 'detail',
                'module'  => 'cms',
                'field'   => config('cms.field_select'),
                'simple'  => cmsValue(config('cms.total_tag_'.$this->site['action'])),
                'sort'    => $term['sortName'],
                'order'   => $term['sortOrder'],
                'paginate'=> [
                    'list_rows' => $term['pageSize'],
                    'page'      => $term['pageNumber'],
                    'path'      => $term['pagePath'],
                ]
            ]);
        }
        //返回结果
        return $term;
    }
}